#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <stdbool.h>  // bool
#include <stdarg.h>   // va_list


#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <fcntl.h>
#include <sys/shm.h>

#include <signal.h>

#include <pthread.h>
#include <ctype.h>


#include "D_LOG.h"
#include "fileinfo.h"


#define MYPORT  12106
#define FILEPORT  12107

#define QUEUE   20


class NetServer {
    private:
        int  mPort;
        char mIp[15];

        int mSockfd;
        int mConn;

    public:
        NetServer(int port = 12106) : mPort(port){
            mPort = port;
            CreateSockfd();
        };

        ~NetServer() {
            //printf("destory conn\n");
            close(mConn);
            close(mSockfd);
        }

        void CreateSockfd(){

                mSockfd = socket(AF_INET, SOCK_STREAM, 0);

                ///定义  sockaddr_in
                struct sockaddr_in seraddr;
                seraddr.sin_family      = AF_INET;
                seraddr.sin_addr.s_addr = htonl(INADDR_ANY);  // ip, 这里的 INADDR_ANY 是 0.0.0.0
                seraddr.sin_port        = htons(mPort);    // 端口号

                ///bind，成功返回0，出错返回-1
                int  ret = bind(mSockfd, (struct sockaddr*)&seraddr, sizeof(seraddr));
                if(ret == -1) {
                    perror("bind error");
                    exit(1);
                }

                ///listen，成功返回0，出错返回-1
                ret = listen(mSockfd, QUEUE);
                if(ret == -1) {
                    perror("listen error");
                    exit(1);
                }

        }


        void WaitConnect(){

            ///客户端套接字
            struct sockaddr_in cliaddr;
            socklen_t cli_len = sizeof(cliaddr);


            // 成功返回非负描述字, 出错返回-1
            // 阻塞等待连接
            mConn = accept(mSockfd, (struct sockaddr*)&cliaddr, &cli_len);
            if(mConn<0)
            {
               perror("client connect error !");
            } else {
               printf("%d client port: %d\n", mConn, ntohs(cliaddr.sin_port));
            }
        }

        int getClientHandle(){
            return mConn;
        }

        int operator*(){
            return mConn;
        }
};

void handleSignal(int signal) {
    if (signal == SIGINT) {
        printf("Program Exit\n");
        exit(1);
    }
}

int main()
{
    signal(SIGINT, handleSignal);
    NetServer appConn(MYPORT);
    while(1){
        appConn.WaitConnect();
        struct MMSG msg;
        //int i = 2225;
        int recvRes = recv(*appConn, &msg, sizeof(msg), 0);
        //recv(appConn->getClientHandle(), &i, sizeof(i), 0);
        //MY_LOGD("%d", i);

        MY_LOGD("%d %d %s",msg.source_id, msg.target_id, msg.content);

        int msgState = MMSG_STATE_FAILD;
        if(recvRes == sizeof(msg)){
            msgState = MMSG_STATE_OK;
        }
        send(*appConn, &msgState, sizeof(msgState), 0);

    }

    return 0;
}

// 将多个字符串拼接到一个字符数组中
void splicString(char *dst, int len, int argc, ...){
    FUNCTION_IN;
    bzero(dst, len);
    va_list args;
    va_start(args, argc);
    char *next = NULL;
    for(int i =0; i< argc; ++i){
        next = va_arg(args, char*);
        strcat(dst, next);
    }
    va_end(args);
    FUNCTION_OUT;
}


